'use strict'

function detail(id) {
    window.location.href = `detail.html?id=${id}`
}

// 跳转按钮
function add() {
    window.location.href = `createOrEdit.html`
}

// 添加与编辑
function save() {
    let parame = window.location.search
    console.log(parame);
    let obj = {
        // id : $('[name=id]').val(),
        productName: $('[name=productName]').val(),
        price: $('[name=price]').val(),
        stockNum: $('[name=stockNum]').val(),
        supplier: $('[name=supplier]').val(),
        remark: $('[name=remark]').val()
    }

    if (parame !== '') {

        let id = window.location.search.split('?')[1].split('=')[1]
        $.ajax({
            url: `http://localhost:3000/product/${id}`,
            type: "PUT",
            data: JSON.stringify(obj),
            contentType: "application/json",
            success: function () {
                console.log(obj);
            }
        })
    } else {
        $.post('http://localhost:3000/product', obj, (data) => {
            console.log(obj);
        })
    }
    // if(id){
    //     let id = window.location.search.split('?')[1].split('=')[1]
    //     $.ajax({
    //         url:`http://localhost:3000/product/${id}`,
    //         type:"PUT",
    //         data:JSON.stringify(obj),
    //         contentType:"application/json",
    //         success:function(){
    //             console.log(obj);
    //         }
    //     })
    // }else{
    //     $.post('http://localhost:3000/product',obj,(data)=>{
    //         console.log(obj);
    //     })
    // }
    if (confirm('确定保存?')) {
        window.location.href = 'index.html'
    }
}

// 返回按钮
function cancel() {
    window.location.href = 'index.html'
}

// 根据id获取数据
function getById(id) {
    return new Promise(function (resolve, reject) {
        $.get(`http://localhost:3000/product/${id}`, data => {
            resolve(data)
        })
    })
}

// 跳转编辑页,传递id
function Edit(id) {
    window.location.href = `createOrEdit.html?id=${id}`
    // if(confirm('确定修改?')){
    //     let obj = {
    //         id
    //     }
    //     $.ajax({
    //         url:`http://localhost:3000/product/${id}`,
    //         type:"PUT",
    //         data:JSON.stringify(obj),
    //         dataType:"json",
    //         contentType:"application/json",
    //         // Headers:{
    //         //   "Content-Type":"application/json",
    //         //   "X-HTTP-Method-Override":"PUT"  
    //         // },
    //         success:function(data){
    //             console.log("保存成功！");
    //          }
    //       });
    // }   
}

// 删除数据
function delProduct(id) {
    if (confirm('确定删除？')) {
        // let obj = {
        //     id
        // }
        $.ajax({
            url: `http://localhost:3000/product/${id}`,
            type: "DELETE",
            // data:JSON.stringify(obj),
            dataType: "json",
            contentType: "application/json",
            // Headers:{
            //   "Content-Type":"application/json",
            //   "X-HTTP-Method-Override":"PUT"  
            // },
        });
    }
    console.log(id);
    console.log('删除成功！');
    setTimeout(() => {
        window.location.reload()
    }, 100);
}

// 查找
function find() {
    let keyword = $('[name=keyword]').val()

    $('table').html('')
    $.get('http://localhost:3000/product',res=>{
        for (let i = 0; i < res.length; i++) {
            if(res[i].productName.search(keyword) != -1 && keyword.length>0 ){
                let html = `
                <tr>
                    <td>${res[i].id}</td>
                    <td>${res[i].productName}</td>
                    <td>${res[i].price}</td>
                    <td>${res[i].stockNum}</td>
                    <td>${res[i].supplier}</td>
                    <td>${res[i].remark}</td>
                    <td>
                        <input type='button' value='编辑' onclick='Edit(${res[i].id})'>
                        <input type='button' value='删除' onclick='delProduct(${res[i].id})'>
                        <input type='button' value='返回' onclick='cancel()'>
                    </td>
                <tr>
                `
                $('table').append(html)
            }
        }
    })
}

